/** 
 * Copyright 2014 whj All Rights Reserved.
 * 项目名：codegnrt
 * 包名：com.whj.code.dao.mybatis
 * 文件名：MybatisSerializablePlugin.java 
 * 日期：2014-11-25 10:21
 */
package com.whj.code.dao.mybatis;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.plugins.SerializablePlugin;

import java.util.Calendar;
import java.util.Date;

/**
 * 生成序列话.
 * @author wanghj
 *
 */
public class MybatisSerializablePlugin extends SerializablePlugin{
    private FullyQualifiedJavaType serializable;

    public MybatisSerializablePlugin() {
        super();
        serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$
    }

    @Override
    protected void makeSerializable(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType(serializable);
        topLevelClass.addSuperInterface(serializable);

        Field field = new Field();
        field.setFinal(true);
        field.setInitializationString("1L"); //$NON-NLS-1$
        field.setName("serialVersionUID"); //$NON-NLS-1$
        field.setStatic(true);
        field.setType(new FullyQualifiedJavaType("long")); //$NON-NLS-1$
        field.setVisibility(JavaVisibility.PRIVATE);

        context.getCommentGenerator().addFieldComment(field, introspectedTable);
        context.getCommentGenerator().addClassComment(topLevelClass, introspectedTable);

        topLevelClass.addField(field);

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);

        StringBuffer fileCommentSb = new StringBuffer();
        fileCommentSb.append("/**\n");
        fileCommentSb.append(" * Copyright " + year + " whj All Rights Reserved. \n");
        fileCommentSb.append(" * 项目名：\n");
        fileCommentSb.append(" * 包名：")
            .append(context.getJavaModelGeneratorConfiguration().getTargetPackage()).append("\n");
        fileCommentSb.append(" * 文件名：")
            .append(context.getTableConfigurations().get(0).getDomainObjectName()).append(".java\n");
        fileCommentSb.append(" * 日期：").append(new Date()).append("\n");
        fileCommentSb.append(" */");

        topLevelClass.addFileCommentLine(fileCommentSb.toString());
    }
}
